package com.simon.hse.activity.danger;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.UnPlanCheckGetListOutput;
import com.simon.hse.http.entity.UnPlanCheckGetListInput;
import com.simon.hse.interfaces.PopListener;
import com.simon.hse.utils.DateTimeUtils;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 隐患上报
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_danger_report)
public class DangerReportActivity extends BaseActivity implements PopListener {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.iv_common_right)
    ImageView mIv_common_right;

    @ViewInject(R.id.smartrefresh)
    SmartRefreshLayout mSmartRefresh;

    @ViewInject(R.id.recyclerview)
    RecyclerView mRecyclerView;

    @ViewInject(R.id.ll_function_1)
    LinearLayout mLl_function_1;

    @ViewInject(R.id.tv_function_1)
    TextView mTv_function_1;

    @ViewInject(R.id.tv_key_1)
    TextView tv_key_1;

    @ViewInject(R.id.ll_function_2)
    LinearLayout mLl_function_2;

    @ViewInject(R.id.tv_function_2)
    TextView mTv_function_2;

    @ViewInject(R.id.tv_key_2)
    TextView tv_key_2;

    @ViewInject(R.id.ll_function_3)
    LinearLayout mLl_function_3;

    @ViewInject(R.id.tv_function_3)
    TextView mTv_function_3;

    @ViewInject(R.id.tv_key_3)
    TextView tv_key_3;

    @ViewInject(R.id.ll_function_more)
    LinearLayout mLl_function_more;

    @ViewInject(R.id.ll_more)
    LinearLayout mLl_more;

    @ViewInject(R.id.ll_InspectType)
    LinearLayout ll_InspectType;

    @ViewInject(R.id.tv_InspectType)
    TextView tv_InspectType;

    @ViewInject(R.id.tv_InspectType_value)
    TextView tv_InspectType_value;

    @ViewInject(R.id.ll_BeginTime)
    LinearLayout ll_BeginTime;

    @ViewInject(R.id.tv_BeginTime)
    TextView tv_BeginTime;

    @ViewInject(R.id.ll_EndTime)
    LinearLayout ll_EndTime;

    @ViewInject(R.id.tv_EndTime)
    TextView tv_EndTime;

    @ViewInject(R.id.ll_Status)
    LinearLayout ll_Status;

    @ViewInject(R.id.tv_Status)
    TextView tv_Status;

    @ViewInject(R.id.tv_Status_value)
    TextView tv_Status_value;

    private int currentPage = 1, totalPage = 1;
    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<UnPlanCheckGetListOutput.DataBean.PageDataBean> mDataBeanList = new ArrayList<>();

    private boolean isShowMore = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("隐患上报");
        mIv_common_right.setImageResource(R.mipmap.tab_add_icon);

        initRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_common_left, R.id.iv_common_right, R.id.ll_function_1, R.id.ll_function_2, R.id.ll_function_3, R.id.ll_function_more})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.iv_common_right:
                Intent intent = new Intent(DangerReportActivity.this, DangerReportDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("STATUS", "0");
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.ll_function_1:
                initPop(mLl_function_1, mTv_function_1, tv_key_1, MyApplication.getInstance().mFactoryList, this);
                break;
            case R.id.ll_function_2:
                initPop(mLl_function_2, mTv_function_2, tv_key_2, MyApplication.getInstance().mDangerClassifyList, this);
                break;
            case R.id.ll_function_3:
                initPop(mLl_function_3, mTv_function_3, tv_key_3, MyApplication.getInstance().mDangerLevelList, this);
                break;
            case R.id.ll_function_more:
                if (isShowMore) {
                    mLl_more.setVisibility(View.GONE);
                } else {
                    mLl_more.setVisibility(View.VISIBLE);
                }
                isShowMore = !isShowMore;
                break;
        }
    }

    @Event(value = {R.id.ll_InspectType, R.id.ll_BeginTime, R.id.ll_EndTime, R.id.ll_Status
            , R.id.tv_more_confirm, R.id.tv_more_reset})
    private void more(View view) {
        switch (view.getId()) {
            case R.id.ll_InspectType:
                initPop(ll_InspectType, tv_InspectType, tv_InspectType_value, MyApplication.getInstance().mDangerStandardsList, this);
                break;
            case R.id.ll_BeginTime:
                reportDate(tv_BeginTime);
                break;
            case R.id.ll_EndTime:
                reportDate(tv_EndTime);
                break;
            case R.id.ll_Status:
                initPop(ll_Status, tv_Status, tv_Status_value, MyApplication.getInstance().mDangerStateList, this);
                break;
            case R.id.tv_more_confirm:
                mLl_more.setVisibility(View.GONE);
                isShowMore = !isShowMore;
                break;
            case R.id.tv_more_reset:
                tv_InspectType.setText("");
                tv_InspectType.setHint("请选择");
                tv_BeginTime.setText("");
                tv_BeginTime.setHint("请选择");
                tv_EndTime.setText("");
                tv_EndTime.setHint("请选择");
                tv_Status.setText("");
                tv_Status.setHint("请选择");
                break;
        }
    }

    private void initRecyclerView() {
        mSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (currentPage * 10 < totalPage) {
                    currentPage++;
                    initData();
                } else {
                    mSmartRefresh.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mDataBeanList.clear();
                initData();
            }
        });

        LinearLayoutManager llm = new LinearLayoutManager(DangerReportActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(DangerReportActivity.this, R.layout.layout_item_danger_report, mDataBeanList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<UnPlanCheckGetListOutput.DataBean.PageDataBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, UnPlanCheckGetListOutput.DataBean.PageDataBean dataBean) {
                TextView tv_danger_report_title = holder.getView(R.id.tv_danger_report_title);
                TextView tv_danger_report_state = holder.getView(R.id.tv_danger_report_state);
                TextView tv_danger_report_date = holder.getView(R.id.tv_danger_report_date);

                tv_danger_report_title.setText(dataBean.getInspectTypeText());
                String status = dataBean.getStatus();// 状态(0保存/1提交)
                tv_danger_report_state.setText(dataBean.getStatusText());
//                switch (status) {
//                    case "0":
//                        tv_danger_report_state.setText("已保存");
//                        tv_danger_report_state.setBackgroundResource(R.drawable.rect_blue_corner_5);
//                        break;
//                    case "1":
//                        tv_danger_report_state.setText("已上报");
//                        tv_danger_report_state.setBackgroundResource(R.drawable.rect_red_corner_5);
//                        break;
//                    default:
//                        tv_danger_report_state.setText("已审批");
//                        tv_danger_report_state.setBackgroundResource(R.drawable.rect_red_corner_5);
//                        break;
//                }
                if (!TextUtils.isEmpty(dataBean.getTime())) {
                    tv_danger_report_date.setText(DateTimeUtils.convertCustomDate(dataBean.getTime()));
                }
            }
        });

        mCommonEmptyAdapter.setOnItemEmptyClickListener(new CommonEmptyAdapter.OnItemEmptyClickListener() {
            @Override
            public void setOnEmptyClickListener() {
                initData();
            }

            @Override
            public void setOnItemClickListener(View view, int position) {
                if (!isShowMore) {
                    Intent intent = new Intent(DangerReportActivity.this, DangerReportDetailActivity.class);
                    UnPlanCheckGetListOutput.DataBean.PageDataBean pageDataBean = mDataBeanList.get(position);
                    Bundle bundle = new Bundle();
                    bundle.putString("ID", pageDataBean.getId());
                    bundle.putString("STATUS", pageDataBean.getStatus());
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });
        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    private void initData() {
        UnPlanCheckGetListInput input = new UnPlanCheckGetListInput();
        UnPlanCheckGetListInput.ModelBean modelBean = new UnPlanCheckGetListInput.ModelBean();
        // 所属工厂
        String createFactory = tv_key_1.getText().toString();
        if (!TextUtils.isEmpty(createFactory)) {
            modelBean.setCreateFactory(createFactory);
        }
        // 隐患分类
        String riskType = tv_key_2.getText().toString();
        if (!TextUtils.isEmpty(riskType)) {
            modelBean.setRiskType(riskType);
        }
        // 隐患级别
        String riskLevel = tv_key_3.getText().toString();
        if (!TextUtils.isEmpty(riskLevel)) {
            modelBean.setRiskLevel(riskLevel);
        }
        // 排查类别
        String inspectType = tv_InspectType_value.getText().toString();
        if (!TextUtils.isEmpty(inspectType)) {
            modelBean.setInspectType(inspectType);
        }
        // 开始时间
        String beginTime = tv_BeginTime.getText().toString();
        if (!TextUtils.isEmpty(beginTime)) {
            modelBean.setBeginTime(beginTime);
        }
        // 结束时间
        String endTime = tv_EndTime.getText().toString();
        if (!TextUtils.isEmpty(endTime)) {
            modelBean.setEndTime(endTime);
        }
        // 状态
        String status = tv_Status_value.getText().toString();
        if (!TextUtils.isEmpty(status)) {
            modelBean.setStatus(status);
        }
        input.setModel(modelBean);
        input.setLimit(10);
        input.setOffset(10 * (currentPage - 1));

        Call<String> call = HttpManager.getInstance().requestService().DANGER_APPROVAL_LIST(HttpManager.getInstance().requestBody(input));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                UnPlanCheckGetListOutput unPlanCheckGetListOutput = new Gson().fromJson(result, UnPlanCheckGetListOutput.class);
                if (unPlanCheckGetListOutput.getResult().equals("Success")) {
                    totalPage = unPlanCheckGetListOutput.getData().getRowCount();
                    List<UnPlanCheckGetListOutput.DataBean.PageDataBean> dataBeanList = unPlanCheckGetListOutput.getData().getPageData();
                    mDataBeanList.addAll(dataBeanList);
                    mCommonEmptyAdapter.notifyDataSetChanged();
                    mSmartRefresh.finishRefresh();
                    mSmartRefresh.finishLoadMore();
                } else {
                    ToastUtils.getInstance().showShortToastBottom(unPlanCheckGetListOutput.getMessage().toString());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
                mSmartRefresh.finishRefresh();
                mSmartRefresh.finishLoadMore();
            }
        });
    }

    @Override
    public void upData() {
        currentPage = 1;
        mDataBeanList.clear();
        initData();
    }
}
